package com.example.adapter;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bean.NewFragmentBean;
import com.example.fruit.MyApplication;
import com.example.fruit.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class CategoryAdapter extends BaseAdapter {
	private Context context;
	private List<NewFragmentBean> datas;
	private MyApplication app;
	private SQLiteDatabase db;
	private int isSelected=0;

	public CategoryAdapter(Context context, List<NewFragmentBean> datas) {
		this.context = context;
		this.datas = datas;
		app=MyApplication.getApplication();
		db=app.getDbUtils().getWritableDatabase();
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return datas.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		ViewHolder vHolder = null;
		if(convertView==null){
			convertView=LayoutInflater.from(context).inflate(R.layout.item_fragment_allguoshi, null);
			vHolder=new ViewHolder();
			ViewUtils.inject(vHolder, convertView);

			convertView.setTag(vHolder);
		}else{
			vHolder=(ViewHolder)convertView.getTag();
			vHolder.imgView.setImageResource(R.drawable.ic_launcher);

		}

		final NewFragmentBean cData = datas.get(position);



		vHolder.titleTv.setText(cData.getProduct_name());
		vHolder.title2Tv.setText(cData.getVolume());
		
		//"http://api.yi18.net/cook/list";


		vHolder.priceTv.setText("￥" + cData.getPrice());
		vHolder.price2Tv.setText("￥" + cData.getOld_price());
		vHolder.imageViewCate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(context,"成功加入购物车",Toast.LENGTH_SHORT).show();

				Cursor cursor=db.rawQuery("select * from t_cart",null);
				boolean isok = false;
				while(cursor.moveToNext()){
					if(cData.getId()==cursor.getInt(1)) {
//						isSelected++;
						int num = cursor.getInt(6);
						isok = true;
						db.execSQL("update t_cart set number = '"+(num+1)+"' where id ='"+cData.getId()+"'");
					}else{
//					isSelected =1;
						isok = false;

					}
				}
				if(!isok)
					db.execSQL(
							"insert into t_cart(id,title,title2,img,price,number) values(?,?,?,?,?,?)",

							new String[]{""+cData.getId(), cData.getProduct_name(),
									cData.getVolume(), cData.getThum_photo(),
									""+cData.getPrice(),""+1});

			}
		});

		String imgPath =  cData.getThum_photo();


		BitmapUtils bitmapUtils=new BitmapUtils(context);
		bitmapUtils.display(vHolder.imgView,imgPath);
		// 将下载的图片保存到SDCard中
		return convertView;
	}



	class ViewHolder {
		@ViewInject(R.id.img_cate)
		ImageView imgView;
		@ViewInject(R.id.title_cate)
		TextView titleTv;
		@ViewInject(R.id.title2_cate)
		TextView title2Tv;
		@ViewInject(R.id.price1_cate)
		TextView	priceTv;
		@ViewInject(R.id.price2_cate)
		TextView	price2Tv;
		@ViewInject(R.id.image2_cate)
		ImageView imageViewCate;
	}

}
